<!-- 医生主页-----------------------------------------------------------------  -->
<template>
	<view class="institution-container">
		<view class="header">
			<image class="institution-icon" :src="doctorhomeData.avatar ">
			</image>
			<view class="info-wrap">
				<view class="name">
					{{ doctorhomeData.name  }}
					<span class="position">全科护士</span>
				</view>
				<navigator :url="'/homepackge/pages/organizations/organizations?_id=' + doctorhomeData.organizationId"
					v-if="doctorhomeData.organizationId">
					<view class="address">
						所属机构：{{ organizationData?.name  }}
						<image class="arrow-icon" src="/static/arrow-right.png"></image>
					</view>
				</navigator>
				<!-- 所属团队 -----------------------------------------------------------------------不对 -->
				<view class="teamname">
					<!-- <text>
						所属团队：{{ doctorhomeData.teamIds?.name }}
					</text> -->
					<text>
						所属团队：{{ doctorhomeData.teamIds.name }}
					</text>
					<image class="arrow-icon" src="/static/arrow-right.png" v-if="doctorhomeData.teamIds?.length">
					</image>
				</view>

				<view class="number">
					签约人数：<span style="color: #2984F8;">{{ doctorhomeData.signNumber  }}</span>
				</view>
				<view class="contact">
					<text>评分：{{ doctorhomeData.rate  }}</text>
				</view>
			</view>
		</view>
		<!-- 医生简介 -->
		<view class="section">
			<view class="section-title">医生简介</view>
			<view class="intro-content">
				<text>{{ doctorhomeData.intro || "暂无简介" }}</text>
			</view>
		</view>

		<view class="section">
			<view class="section-title">擅长</view>
			<view class="intro-content">
				{{ doctorhomeData.skill || "暂无" }}
			</view>
		</view>

		<!-- 申请签约跳转另一个页面 -->
		<view class="popup-open">
			<button class="Contract-Signing" @click="navigateToSign">申请签约</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		DoctorDetailApi
	} from '../../../api/doctor';
	import {
		organizationsDetailApi
	} from '../../../api/nearbyLnstitutions';

	// 当前医生ID（从页面参数获取）
	const doctorhomeId = ref('');
	// 医生详情数据
	const doctorhomeData = ref({});
	// 机构数据
	const organizationData = ref({});

	// 页面加载时执行
	onLoad(async (options) => {
		console.log('页面参数:', options);
		// 保存医生ID
		doctorhomeId.value = options._id;
		//保存团队ID
		teamId.value = options._id;
		//保存机构的ID
		institutionId.value = options._id;
		if (options._id) {
			await getDoctorDetail(options._id);
		} else {
			uni.showToast({
				title: '缺少医生ID参数',
				icon: 'none'
			});
		}
	});


	// 获取医生详情
	async function getDoctorDetail(id) {
		const res = await DoctorDetailApi({
			_id: id
		});

		if (res.code === 200) {
			doctorhomeData.value = res.data;
			// 如果存在机构ID，获取机构详情
			if (res.data.organizationId) {
				await getOrganizationDetail(res.data.organizationId);
			}
		}

	}

	// 获取机构详情
	async function getOrganizationDetail(id) {
		try {
			const res = await organizationsDetailApi({
				_id: id
			});
			if (res.code === 200) {
				organizationData.value = res.data;
			}
		} catch (err) {
			console.error('获取机构详情失败:', err);
		}
	}

	// 跳转到签约页面
	function navigateToSign() {
		uni.navigateTo({
			url: `/pages/sign-contract/sign-contract?doctorId=${doctorhomeId.value}`
		});
	}
</script>

<style scoped>
	.institution-container {
		padding: 20rpx;
	}

	.header {
		display: flex;
		margin-bottom: 30rpx;
	}

	.institution-icon {
		width: 150rpx;
		height: 150rpx;
		border-radius: 10rpx;
		margin-right: 20rpx;
	}

	.info-wrap {
		flex: 1;
	}

	.name {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.position {
		font-size: 24rpx;
		color: #666;
		margin-left: 15rpx;
	}

	.address,
	.teamname,
	.number,
	.contact {
		font-size: 26rpx;
		color: #666;
		margin-bottom: 8rpx;
		display: flex;
		align-items: center;
	}

	.arrow-icon {
		width: 24rpx;
		height: 24rpx;
		margin-left: 10rpx;
	}

	.section {
		margin-bottom: 30rpx;
	}

	.section-title {
		font-size: 30rpx;
		font-weight: bold;
		margin-bottom: 15rpx;
	}

	.intro-content {
		font-size: 26rpx;
		color: #666;
		line-height: 1.6;
	}

	.Contract-Signing {
		background-color: #2984F8;
		color: white;
		border-radius: 50rpx;
		margin-top: 40rpx;
	}

	.popup-open {
		padding: 0 30rpx;
	}
</style>